package com.focus.xyz.union.pages.frontend;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BasePage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.util.List;

/**
 * Created by chenmeng on 2016/3/30.
 */
public class RegisterPage extends BasePage {
    private Action action;
    @FindAll({
            @FindBy(xpath = "//*[@id=\"dev_wzz\"]"),
            @FindBy(xpath = "//*[@id=\"dev_dlr\"]")
    })
    private List<WebElement> memRoleCheckBox;
    @FindAll({
            @FindBy(xpath = "//*[@id=\"personRadio\"]"),
            @FindBy(xpath = "//*[@id=\"companyRadio\"]")
    })
    private List<WebElement> memRoleTypeRadio;
    @FindBy(xpath = "//*[@id=\"dev_loginName\"]")
    private WebElement userName;
    @FindBy(xpath = "//*[@id=\"dev_email\"]")
    private WebElement mailAddress;
    @FindBy(xpath = "//*[@id=\"dev_loginPwd\"]")
    private WebElement pass;
    @FindBy(xpath = "//*[@id=\"dev_loginPasswordConfirm\"]")
    private WebElement passConfirm;

    @FindBy(xpath = "//*[@id=\"dev_person_name_txt\"]")
    private WebElement name;
    @FindBy(xpath = "//*[@id=\"dev_mobile\"]")
    private WebElement mobile;
    @FindBy(xpath = "//*[@id=\"dev_cardNo\"]")
    private WebElement idNo;
    @FindBy(xpath = "//*[@id=\"dev_agentNo\"]")
    private WebElement proxyCert;
    @FindBy(xpath = "//a[contains(text(),'新一站网站联盟用户协议')]")
    private WebElement userProtocolLink;

    @FindBy(xpath = "//*[@id=\"dev_submit_button\"]")
    private WebElement registerBtn;

    public RegisterPage(WebDriver driver,Action action){
        super(driver,action);
        this.action = action;
        PageFactory.initElements(driver,this);
    }

    public void selectRole(String role) throws Exception{
        if(this.memRoleCheckBox!=null&&this.memRoleCheckBox.size() == 2){
            WebElement wzz = this.memRoleCheckBox.get(0);
            WebElement dlr = this.memRoleCheckBox.get(1);
            if("网站主".equals(role)){
                action.selectCheckBox(wzz);
                action.deselectCheckBox(dlr);
            }else if("保险代理人".equals(role)){
                action.selectCheckBox(dlr);
                action.deselectCheckBox(wzz);
            }else if("ALL".equals(role)){
                action.selectCheckBox(wzz);
                action.selectCheckBox(dlr);
            }
        }
    }

    public void selectRoleType(String roleType) throws Exception{
        if(this.memRoleTypeRadio!=null&&this.memRoleTypeRadio.size() == 2){
            WebElement p = this.memRoleTypeRadio.get(0);
            WebElement c = this.memRoleTypeRadio.get(1);
            if("个人会员".equals(roleType)){
                p.click();
            }else if("企业会员".equals(roleType)){
                c.click();
            }
        }
    }

    public void inputUserName(String userName) throws Exception{
        action.inputText(this.userName,userName);
    }

    public void inputEmail(String email) throws Exception{
        action.inputText(mailAddress,email);
    }

    public void inputMobile(String mobile) throws Exception{
        action.inputText(this.mobile,mobile);
    }

    public void inputPass(String pass) throws Exception{
        action.inputText(this.pass,pass);
    }
    public void confirmPass(String confirmPass) throws Exception{
        action.inputText(passConfirm,confirmPass);
    }

    public void inputName(String name) throws Exception{
        action.inputText(this.name,name);
    }

    public void inputIdNo(String idNo) throws Exception{
        action.inputText(this.idNo,idNo);
    }

    public void inputProxyCert(String cert) throws Exception{
        action.inputText(proxyCert,cert);
    }

    public void clickRegistBtn() throws Exception{
        action.click(registerBtn);
    }

    public void register(String memRole,String roleType,String userName,String email,String pass,String name,String bill,String idNo) throws Exception{
        selectRole(memRole);
        selectRoleType(roleType);
        inputUserName(userName);
        inputEmail(email);
        inputPass(pass);
        confirmPass(pass);
        inputName(name);
        inputMobile(bill);
        inputIdNo(idNo);
        inputProxyCert(idNo);
        clickRegistBtn();
    }

}
